package com.vanguard.service.impl;

import com.vanguard.commons.exception.BusinessException;
import com.vanguard.domain.Appointment;
import com.vanguard.domain.vo.AppointmentVo;
import com.vanguard.mapper.AppointmentMapper;
import com.vanguard.service.IAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Title: 预约相关业务实现类
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2018/11/16
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public void save(Appointment appointment) {
        try {
            appointment.setApplyTime(new Date());
            appointment.setApplyStatus(0);
            appointmentMapper.insert(appointment);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    @Override
    public List<AppointmentVo> findByOpenId(String openId) {
        List<AppointmentVo> appointmentList = null;
        try {
            appointmentList = appointmentMapper.findByOpenId(openId);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return appointmentList;
    }

    @Override
    public AppointmentVo findByAppointId(Integer id) {
        AppointmentVo appointment = null;
        try {
            appointment = appointmentMapper.findByAppointId(id);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
        return appointment;
    }

    @Override
    public void cancelAppoint(Integer id) {
        Appointment appointment = new Appointment();
        appointment.setId(id);
        //更新状态为取消预约
        appointment.setApplyStatus(2);
        try {
            appointmentMapper.updateByPrimaryKeySelective(appointment);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }
}
